// packageF/wxFriendExtend/friendRanking/friendRanking.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity_id: 0,
    details: null,

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    noMore: false, //是否没有更多了
    total_page: 0, //总页数
    listData: [],

    activityPosterList:[],
    isViewedPosterList:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        activity_id: options.id
      });
    }
    this._getData();
  },

  _getData() {
    let urlStr = app.getNetAddresss("plugin.customer-increase.frontend.activity.activityMemberRank");
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.activity_id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.last_page;

        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        if (this.data.page >= this.data.total_page) {
          this.setData({
            noMore: true
          });
        }
        let   activityPosterList = []
        if(res.data.data && res.data.data.length!==0){
          activityPosterList  =  res.data.data.slice(0,6);
        }
        
        this.setData({
          activityPosterList,
          details: res.data,
          listData:res.data.data,
        });
      },
    });
  },

  //加载更多数据
  loadMore() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss('plugin.customer-increase.frontend.activity.activityMemberRank');
      let json = {
        page: this.data.page,
        id: this.data.activity_id
      };

      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({
              listData
            });
            if (this.data.page >= this.data.total_page) {
              this.setData({
                noMore: true
              });
            }
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  gotoInvitation() {
    wx.navigateTo({
      url: '/packageF/wxFriendExtend/invitationRecord/invitationRecord?id=' + this.data.activity_id,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isViewedPosterList==false){
      this.setData({activityPosterList:this.data.listData,isViewedPosterList:true});
      this.loadMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let json = {
      title: "好友裂变排行榜",
      path: "/packageF/wxFriendExtend/workWxFriendExtend/workWxFriendExtend?id=" + this.data.activity_id
    };
    return json;
  }
});